package wms2010.gui.forms.ageClass;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import wms2010.controller.controller.IAgeClassController;
import wms2010.controller.controller.IAgeClassControllerCallback;
import wms2010.controller.controller.IDisciplineController;
import wms2010.gui.views.ageClass.DisciplineViewTableModel;
import wms2010.gui.views.disciplines.DisciplineComboboxModel;
import wms2010.gui.views.disciplines.DisciplineViewModel;
import wms2010.model.controller.model.AgeClass;
import wms2010.model.controller.model.Discipline;

@SuppressWarnings("serial")
@Component
public class AgeClassFormCreate extends AbstractAgeClassForm implements
		IAgeClassControllerCallback, Serializable {
	// private DisciplineViewTableModel discTableModel = new
	// DisciplineViewTableModel();

	@Autowired
	private IAgeClassController iAgeClassController;
	@Autowired
	private IDisciplineController iDisciplineController;

	private DisciplineComboboxModel comboboxModel;
	private DisciplineViewTableModel disciplineViewTableModel;

	public IAgeClassController getAgeController() {
		return iAgeClassController;
	}

	public void setAgeController(IAgeClassController ageController) {
		this.iAgeClassController = ageController;
	}

	@Autowired
	public AgeClassFormCreate(
			DisciplineViewTableModel disciplineViewTableModel,
			DisciplineComboboxModel comboboxModel) {
		super(disciplineViewTableModel, comboboxModel);

		this.comboboxModel = comboboxModel;
		this.disciplineViewTableModel = disciplineViewTableModel;

		this.addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addClicked();
			}
		});

		this.removeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				removeClicked();
			}
		});

		this.okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				okClicked();
			}
		});
	}

	private void okClicked() {
		try {
			iAgeClassController.setCallback(this);
			AgeClass ageClass = iAgeClassController.createAgeClass("abc", this.disciplineViewTableModel.getDisciplines());
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private void addClicked() {
		Discipline discipline = ((DisciplineViewModel) (this.comboboxModel
				.getSelectedItem())).getDiscipline();

		this.disciplineViewTableModel.addDiscipline(discipline);
	}

	private void removeClicked() {
		Discipline discipline = ((DisciplineViewModel) (this.comboboxModel
				.getSelectedItem())).getDiscipline();
		this.disciplineViewTableModel.removeDiscipline(discipline);
	}

	@Override
	public void show() {
		super.show();
		List<Discipline> disciplines;
		try {
			disciplines = this.iDisciplineController.getAllDisciplines();
			this.comboboxModel.setDisciplineList(disciplines);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void hello() {
		System.out.println("helo");
	}
}
